package com.atguigui.leetcode1;

/**
 * 2544. 交替数字和
 * Project: leetcode
 * Package: com.atguigui.leetcode1
 * Version: 1.0
 * <p>
 * Created by WJX on 2023/7/12 8:40
 */
public class P2544AlternatingDigitSum {
    public static void main(String[] args) {
        Solution solution = new P2544AlternatingDigitSum().new Solution();
        solution.alternateDigitSum(521);
    }


    class Solution {
        public int alternateDigitSum(int n) {
            String num = n + "";
            int m = num.length();
            int ans = 0;
            for (int i = 0; i < m; i++) {
                if (i % 2 == 0) {
                    ans += Integer.valueOf(num.substring(i, i + 1));
                } else {
                    ans -= Integer.valueOf(num.substring(i, i + 1));
                }

            }
            return ans;
        }
    }
}
